<?php

namespace App\Common\Lib\Hyperf\QueryExtend\Query;

use App\Common as com;
use App\Common\Lib\Hyperf\QueryExtend\Query\Builder as nameBuilder;
use Hyperf\Database\Query\Builder as BaseBuilder;
use Hyperf\Utils\Collection;
use App\Common\Lib\Hyperf\QueryExtend\Contracts\QueryCacheModuleInterface;
use App\Common\Lib\Hyperf\QueryExtend\Traits\QueryCacheModule;

class Builder extends BaseBuilder implements QueryCacheModuleInterface {
	use QueryCacheModule;

	/**
	 * {@inheritdoc}
	 */
	public function get( $columns = [ '*' ] ):Collection {
		return $this->shouldAvoidCache() ? parent::get( $columns ) : $this->getFromQueryCache( 'get', $columns );
	}

	/**
	 * {@inheritdoc}
	 */
	public function useWritePdo() {
		// Do not cache when using the write pdo for query.
		$this->doNotCache();

		// Call parent method
		parent::useWritePdo();

		return $this;
	}

	/**
	 * Add a subselect expression to the query.
	 *
	 * @param \Closure|$this|string $query
	 * @param string $as
	 * @return $this
	 *
	 * @throws \InvalidArgumentException
	 */
	public function selectSub( $query, $as ) {
		if( get_class( $query ) == self::class ){
			$this->appendCacheTags( $query->getCacheTags() ?? [] );
		}

		return parent::selectSub( $query, $as );
	}
}
